package ie.dkit.semanticPhotoWeb.client.panels;

import ie.dkit.semanticPhotoWeb.client.Main;
import ie.dkit.semanticPhotoWeb.client.dtos.AlbumDTO;
import ie.dkit.semanticPhotoWeb.client.dtos.ImageDTO;
import ie.dkit.semanticPhotoWeb.client.tools.AsyncCallbackAdapter;

import com.google.gwt.user.client.HistoryListener;
import com.gwtext.client.core.RegionPosition;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.layout.BorderLayout;
import com.gwtext.client.widgets.layout.BorderLayoutData;

/**
 * @author Sebastian Ruehl
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
public final class MainPanel extends Panel implements HistoryListener {

	private final Main m_Main;

	private CenterPanel m_CenterPanel;

	private StatusPanel m_StatusPanel;

	private MainToolbar m_MainToolbar;

	public MainPanel(Main main) {
		m_Main = main;
		setLayout(new BorderLayout());
		add(getCenterPanel(), new BorderLayoutData(RegionPosition.CENTER));
		add(getStatusPanel(), new BorderLayoutData(RegionPosition.SOUTH));
		setTopToolbar(getMainToolbar());
	}

	public CenterPanel getCenterPanel() {
		if (m_CenterPanel == null) {
			m_CenterPanel = new CenterPanel(m_Main);
		}
		return m_CenterPanel;
	}

	public StatusPanel getStatusPanel() {
		if (m_StatusPanel == null) {
			m_StatusPanel = new StatusPanel();
		}
		return m_StatusPanel;
	}

	public MainToolbar getMainToolbar() {
		if (m_MainToolbar == null) {
			m_MainToolbar = new MainToolbar(m_Main, getCenterPanel());
		}
		return m_MainToolbar;
	}

	public void onHistoryChanged(String historyToken) {
		if (historyToken.equals("")) {
			return;
		}
		String[] components = historyToken.split("/");
		switch (components.length) {
		case 0:
			return;
		case 1:
			getCenterPanel().addUserOverviewPanel(components[0]);
			break;
		case 2:
			addAlbumPanel(components[0], components[1]);
			break;
		case 3:
			addImagePanel(components[0], components[1], components[2]);
			break;
		default:
			return;
		}
	}

	private void addAlbumPanel(String username, String albumName) {
		m_Main.getClientServerConnectorAsync().getAlbumByName(username,
				albumName, new AsyncCallbackAdapter() {

					public void onSuccess(Object result) {
						getCenterPanel().addAlbumPanel((AlbumDTO) result);
					}

				});
	}

	private void addImagePanel(String username, String albumName, String imageID) {
		m_Main.getClientServerConnectorAsync().getImageByID(username,
				albumName, imageID, new AsyncCallbackAdapter() {

					public void onSuccess(Object result) {
						getCenterPanel().addPhotoPanel((ImageDTO) result);
					}

				});
	}
}
